<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp"%>
<script type="text/javascript">
$(function() {
	 var roleList = ${roleList};
     parent.$.messager.progress('close');
     $('#dataGrid').datagrid({
         nowrap: false,
         striped: true,
         queryParams:{}, //查询条件  
         url: contextPath + '/role/getRoleListData.action',
         sortName: 'id',
         sortOrder: 'asc',
         loadMsg:'数据加载中请稍后...', 
         remoteSort: false,
         idField:'id',
         /* pageList:[10], */
         fitColumns:true,
         pagination:true,
         rownumbers:true,
         height:308,
         scrollbarSize:0,
         columns:[[
             {field:'ck',checkbox:true},
             {title:'编号',field:'id',width:35,sortable:true,
                 sorter:function(a,b){
                     return (a>b?1:-1);
                 }
             },
             {title:'名称',field:'name',width:100,sortable:true},
             {title:'描述',field:'description',width:150,sortable:true}
         ]],
         onLoadSuccess:function(data){ 
        	   $.each(roleList,function(index,obj){
        		   $('#dataGrid').datagrid("selectRecord",obj.id);
        	   });
         },
         onSelect:function(rowIndex, rowData){
        	 updateUserRole(rowData.id,"");
         },
         onUnselect:function(rowIndex, rowData){
             updateUserRole("",rowData.id);
         },
         onSelectAll:function(rows){
        	 var roleIds = [];
        	 for(var i = 0;i < rows.length;i++){
        		 roleIds.push(rows[i].id);
        	 }
        	 updateUserRole(roleIds.join(','),"");
         },
         onUnselectAll:function(rows){
        	 var roleIds = [];
             for(var i = 0;i < rows.length;i++){
                 roleIds.push(rows[i].id);
             }
             updateUserRole("",roleIds.join(','));
         }
     });
});

//更新用户分配角色信息
function updateUserRole(checkRoleIds,unCheckRoleIds){
    $.ajax({
        type : "post",
        url : contextPath + "/user/assignRoles.action",
        data : {
        	'user.Id' : "${user.id}",
            'checkRoleIds' : checkRoleIds,
            'uncheckRoleIds' : unCheckRoleIds
        },
        datatype: "json",
        success : function(data) {
            //alert(data);
        },
        error :parent.commonAjaxError
    });
}
</script>
<!-- edit-dialog begin -->
<div class="main">
    <div style="margin: -1px 1px 0 -1px;">
        <table id="dataGrid" data-options="toolbar:'#dg-toolbar'"></table>
    </div>
</div> 
<!-- edit-dialog end -->